﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Security.Permissions;

using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.UserCode;
using Microsoft.SharePoint.Workflow;

namespace EFSBWFActivities
{
    [ToolboxItemAttribute(true)]
    public class buildLinkFieldValueStringSB
    {
        #region Logging
        //[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static void LogDebugInfo(SPUserCodeWorkflowContext context, string strMessage)
        {
#if DEBUG
            Log(context, strMessage);
#endif
        }

        //[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static void Log(SPUserCodeWorkflowContext context, string strMessage)
        {
            using (SPSite site = new SPSite(context.CurrentWebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPWorkflow.CreateHistoryEvent(web, context.WorkflowInstanceId, 0,
                        web.CurrentUser, TimeSpan.Zero, "buildLinkFieldValueStringSB", strMessage, string.Empty);
                }
            }
        }

        #endregion

        //[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static Hashtable Execute(SPUserCodeWorkflowContext context, string Description, string URL)
        {
            Hashtable results = new Hashtable();
            results["Except"] = string.Empty;
            results["Status"] = "Success";

            string strLinkFieldValue = string.Empty;
            string strTmp = string.Empty;

            LogDebugInfo(context, @"Description=" + Description);
            LogDebugInfo(context, @"URL=" + URL);

            if (string.IsNullOrEmpty(Description))
                Description = @"(empty)";
            if (string.IsNullOrEmpty(URL))
            {
                results["LinkFieldValue"] = strLinkFieldValue;
                return results;
            }

            SPFieldUrlValue objSPFieldUrlValue = new SPFieldUrlValue();
            objSPFieldUrlValue.Description = Description;
            objSPFieldUrlValue.Url = URL;

            strLinkFieldValue = objSPFieldUrlValue.ToString();
            results["LinkFieldValue"] = strLinkFieldValue;

            LogDebugInfo(context, string.Format(@"strLinkFieldValue={0}", strLinkFieldValue));

            return results;
        }
    }
}
